﻿using System;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Linq.Expressions;
using System.Collections.Generic;
using System.Threading.Tasks;
using YiSha.Util;
using YiSha.Util.Extension;
using YiSha.Util.Model;
using YiSha.Data;
using YiSha.Data.Repository;
using YiSha.Entity.QixingManage;
using YiSha.Model.Param.QixingManage;
using YiSha.Model.Result.QixingManage;

namespace YiSha.Service.QixingManage
{
    /// <summary>
    /// 创 建：admin
    /// 日 期：2023-04-29 21:16
    /// 描 述：退货申请处理服务类
    /// </summary>
    public class OrderRefundApplyService :  RepositoryFactory
    {
        #region 获取数据
        public async Task<List<OrderRefundApplyEntity>> GetList(OrderRefundApplyListParam param)
        {
            var expression = ListFilter(param);
            var list = await this.BaseRepository().FindList(expression);
            return list.ToList();
        }

        public async Task<List<OrderRefundApplyEntity>> GetPageList(OrderRefundApplyListParam param, Pagination pagination)
        {
            var expression = ListFilter(param);
            var list= await this.BaseRepository().FindList(expression, pagination);
            return list.ToList();
        }

        public async Task<OrderRefundApplyEntity> GetEntity(long id)
        {
            return await this.BaseRepository().FindEntity<OrderRefundApplyEntity>(id);
        }

        public async Task<OrderRefundApplyDto> GetOrderRefundApplyFormJson(long id)
        {

            StringBuilder sql = new StringBuilder();

            sql.AppendFormat(@" select a.*,b.nickname,c.address_name,c.name,c.phone,c.province,c.city,c.region,c.detail_address from order_refund_apply  a
                LEFT JOIN customer_user b ON a.openid = b.openid
                LEFT JOIN oms_company_address c on a.company_address_id = c.Id  where a.Id={0} ", id);

            var refund=await this.BaseRepository().FindObject<OrderRefundApplyDto>(sql.ToString());

            if (refund!=null)
            {
                var refunditems = this.BaseRepository().IQueryable<RefundItemsEntity>(t => t.refund_id == refund.Id).ToList();

                foreach (var item in refunditems)
                {
                    var warehouseEntity = this.BaseRepository().IQueryable<GoodsWarehouseEntity>(t => t.Id == item.pw_id).FirstOrDefault();
                    item.VIN = warehouseEntity?.VIN;
                }

                refund.itemsEntities = refunditems;
            }

            return refund;
        }
        
        #endregion

        #region 提交数据
        public async Task SaveForm(OrderRefundApplyEntity entity)
        {
            if (entity.Id.IsNullOrZero())
            {
                entity.Create();
                await this.BaseRepository().Insert(entity);
            }
            else
            {
                
                await this.BaseRepository().Update(entity);
            }
        }

        public async Task DeleteForm(string ids)
        {
            long[] idArr = TextHelper.SplitToArray<long>(ids, ',');
            await this.BaseRepository().Delete<OrderRefundApplyEntity>(idArr);
        }
        #endregion

        #region 私有方法
        private Expression<Func<OrderRefundApplyEntity, bool>> ListFilter(OrderRefundApplyListParam param)
        {
            var expression = LinqExtensions.True<OrderRefundApplyEntity>();
            if (param != null)
            {
                if (!string.IsNullOrEmpty(param.ordersn))
                {
                    expression = expression.And(t => t.ordersn.Contains(param.ordersn));
                }
                if (!string.IsNullOrEmpty(param.StartTime.ParseToString()))
                {
                    expression = expression.And(t => t.createtime >= param.StartTime);
                }
                if (!string.IsNullOrEmpty(param.EndTime.ParseToString()))
                {
                    param.EndTime = param.EndTime.Value.Date.Add(new TimeSpan(23, 59, 59));
                    expression = expression.And(t => t.createtime <= param.EndTime);
                }
                if (!string.IsNullOrEmpty(param.PStartTime.ParseToString()))
                {
                    expression = expression.And(t => t.processtime >= param.PStartTime);
                }
                if (!string.IsNullOrEmpty(param.PEndTime.ParseToString()))
                {
                    param.PEndTime = param.PEndTime.Value.Date.Add(new TimeSpan(23, 59, 59));
                    expression = expression.And(t => t.processtime <= param.PEndTime);
                }
                if (!param.tenant_id.IsNullOrZero())
                {
                    expression = expression.And(t => t.tenant_id==param.tenant_id);
                }
            }
            return expression;
        }
        #endregion
    }
}
